// pages/workOrder/createOrder/index.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'

// let icons = ['/assets/icon-bank-pf.png', '/assets/icon-bank-gs.png', '/assets/icon-bank-js.png', '/assets/icon-bank-jt.png']

Page({

    /**
     * 页面的初始数据
     */
    data: {
        workOrderGrids: [],
        currentId: '',
        actions: [{
                name: '现金支付',
                color: '#2d8cf0',
            },
            {
                name: '微信支付',
                color: '#19be6b'
            },
            {
                name: '取消'
            }
        ],
    },

    /**点击选择格子 */
    clickGridItem({ currentTarget }) {
        this.setData({
            currentId: currentTarget.id
        })
    },

    /**正式创建工单并进入第一阶段 */
    createOrderToNext(e) {
        console.log(e)
        if (!this.data.currentId) return;

        wx.showModal({
            title: '是否确认创建当前平台工单？',
            content: '工单创建后将无法撤销，请谨慎操作',
            confirmText: '创建工单',
            cancelText: '放弃',
            success: res => {
                if (res.confirm) {

                    this.createWorkOrder()
                }
            }
        })

    },

    /**请求创建工单 */
    createWorkOrder() {
        let param = { "onlineCompanyId": this.data.currentId === '-1' ? '' : this.data.currentId, "openid": this.data.openid }
        requestUtil.postRequest(urls.workOrder.createOrder, param, (res) => {
            console.log(res)
            let workOrderId = res
            let workOrderProcess = 0
            wx.redirectTo({
                url: '/pages/formTemplate/index?orderId=' + workOrderId + '&process=' + workOrderProcess
            })
        }, () => {})
    },
    /**请求平台列表 */
    getPlantList() {
        requestUtil.postRequest(urls.workOrder.getPlantList, { openid: this.data.openid }, (res) => {
            // let list = res
            // list.forEach((item) => {
            //     // let i = Math.round(Math.random() * 3)
            //     // item.icon = icons[i]
            //     item.text = item.name
            //     console.log(item, i)
            //     return item
            // })
            this.setData({
                workOrderGrids: res.online,
                rescueCompany: res.rescueCompany
            })
        }, () => {})
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let openid = wx.getStorageSync('openid')
        this.setData({
            openid: openid
        })
        this.getPlantList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})